Imports System.IO
Imports System.Drawing
Imports System.Data.SqlClient
Imports System.Web.UI
Imports System.Collections.Generic
Imports System.Reflection
Imports System.Threading

Imports DotNetNuke
Imports DotNetNuke.Common
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Common.Utilities.UrlUtils
Imports DotNetNuke.Data
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Security.Roles
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Services.Localization
Imports DotNetNuke.Entities.Modules.Communications

Imports Microsoft.VisualBasic


Namespace TressleWorks.Modules.ThermoChart

    Partial Class ThermoChartSettings
        Inherits Entities.Modules.PortalModuleBase

        Dim oInfo As ThermoChartInfo = Nothing

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try

                oInfo = New ThermoChartInfo(Me)
                Call oInfo.Load()

                If Not Page.IsPostBack Then
                    Call LoadFields()
                End If

            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Private Sub cmdCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdCancel.Click
            Try
                Response.Redirect(NavigateURL(), True)
            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Private Sub cmdUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdUpdate.Click
            Try
                Call SaveFields()
                Call PurgeChartImages()

                ' Redirect back to the portal home page
                Response.Redirect(NavigateURL(), True)
            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub


#Region "Private Routines"
        Private Sub LoadFields()

            If oInfo.IsNewChart Then

                oInfo.ShowBulb = True
                oInfo.ShowValue = True
                oInfo.ShowRoundedTip = True
                oInfo.ShowLines = False
                oInfo.ShowShadow = True

                oInfo.TitleFontSettingsString = "Arial, 14pt, Gray, Bold"

                oInfo.CurrentValue = 0
                oInfo.ValueFontSettingsString = "Arial, 14pt, blue"

                oInfo.MinValue = 0
                oInfo.MaxValue = 100
                oInfo.MajorMarkerCount = 10
                oInfo.MinorMarkerCount = 1
                oInfo.ScaleFontSettingsString = "Arial, 11pt, Gray, Bold"

                oInfo.MercuryColourSettingsString = "Red"
                oInfo.MercuryShadowSettingsString = "Silver"
                oInfo.BackGroundSettingsString = "White"

                oInfo.Height = 400
                oInfo.Width = 240

                oInfo.MarginLeft = 20
                oInfo.MarginRight = 20
                oInfo.MarginTop = 20
                oInfo.MarginBottom = 20

                oInfo.CenterLineAdjust = 0
            End If

            txtTitle.Text = oInfo.Title
            txtTitleFontSettings.Text = oInfo.TitleFontSettingsString

            txtMinValue.Text = oInfo.MinValue.ToString("0.0")
            txtMaxValue.Text = oInfo.MaxValue.ToString("0.0")
            txtMajorMarkerCount.Text = oInfo.MajorMarkerCount.ToString
            txtMinorMarkerCount.Text = oInfo.MinorMarkerCount.ToString
            txtMarkerSizes.Text = oInfo.MarkerSizeString

            txtScaleFontSettings.Text = oInfo.ScaleFontSettingsString
            txtScaleFormat.Text = oInfo.ScaleFormatString

            chkShowValue.Checked = oInfo.ShowValue
            chkShowValueAsPercent.Checked = oInfo.ShowValueAsPercent
            txtCurrentValue.Text = oInfo.CurrentValue.ToString("0.0")
            txtValueFontSettings.Text = oInfo.ValueFontSettingsString
            txtValueFormat.Text = oInfo.ValueFormatString

            txtHeight.Text = oInfo.Height.ToString
            txtWidth.Text = oInfo.Width.ToString

            txtMarginSizes.Text = oInfo.MarginSizeString.ToString
            txtShaftWidth.Text = oInfo.BarWidth.ToString
            txtCenterLineAdjust.Text = oInfo.CenterLineAdjust.ToString

            chkShowRoundedTip.Checked = oInfo.ShowRoundedTip
            chkShowBulb.Checked = oInfo.ShowBulb
            chkShowShadow.Checked = oInfo.ShowShadow
            chkShowGradientOverRange.Checked = oInfo.ShowGradientOverRange

            txtMercuryColourSettings.Text = oInfo.MercuryColourSettingsString
            txtShadowColorSettings.Text = oInfo.MercuryShadowSettingsString
            txtBackgroundColorSettings.Text = oInfo.BackGroundSettingsString

            txtHeader.Text = oInfo.Header
            txtFooter.Text = oInfo.Footer

            txtIMCParameterName.Text = oInfo.IMCParameterName

            chkShowSQLCommand.Checked = oInfo.ShowSQLCommand
            chkEnableSQL.Checked = oInfo.EnableSQL
            txtMatrixColumns.Text = oInfo.MatrixColumns.ToString
            txtMatrixRows.Text = oInfo.MatrixRows.ToString
            txtSQLCommand.Text = oInfo.SQLCommand
            txtConnectionString.Text = oInfo.ConnectionString

            Call LoadDropDownLists(oInfo)

        End Sub

        Private Sub SaveFields()

            oInfo.Title = txtTitle.Text
            oInfo.TitleFontSettingsString = txtTitleFontSettings.Text

            Dim dMin As Decimal = Val(txtMinValue.Text)
            Dim dMax As Decimal = Val(txtMaxValue.Text)

            If dMin > dMax Then
                lblMessage.Text = "Minimun Value exceeds Maximum Value"
                lblMessage.Visible = True
                Exit Sub
            ElseIf dMax < dMin Then
                lblMessage.Text = "Maximum value less than Minimum Value"
                lblMessage.Visible = True
                Exit Sub
            End If

            'oInfo.ShowScale = chkShowScale.Checked
            oInfo.MinValue = dMin
            oInfo.MaxValue = dMax
            oInfo.MajorMarkerCount = Val(txtMajorMarkerCount.Text)
            oInfo.MinorMarkerCount = Val(txtMinorMarkerCount.Text)
            oInfo.MarkerSizeString = txtMarkerSizes.Text
            oInfo.ScaleFontSettingsString = txtScaleFontSettings.Text

            oInfo.ShowValue = chkShowValue.Checked
            oInfo.ShowValueAsPercent = chkShowValueAsPercent.Checked
            oInfo.CurrentValue = Val(txtCurrentValue.Text)
            oInfo.ValueFontSettingsString = txtValueFontSettings.Text

            oInfo.Height = Val(txtHeight.Text)
            oInfo.Width = Val(txtWidth.Text)

            oInfo.MarginSizeString = txtMarginSizes.Text
            oInfo.BarWidth = Val(txtShaftWidth.Text)
            oInfo.CenterLineAdjust = Val(txtCenterLineAdjust.Text)

            oInfo.ScaleLocation = System.Enum.Parse(GetType(ThermoChartInfo.ScaleLocationType), ddlScaleLocation.SelectedValue)

            oInfo.ValueFormat = System.Enum.Parse(GetType(ThermoChartInfo.ValueFormatType), ddlValueType.SelectedValue)
            oInfo.ValueFormatString = txtValueFormat.Text
            oInfo.ScaleFormatString = txtScaleFormat.Text

            oInfo.ShowBulb = chkShowBulb.Checked
            oInfo.ShowShadow = chkShowShadow.Checked
            oInfo.ShowRoundedTip = chkShowRoundedTip.Checked
            oInfo.ShowGradientOverRange = chkShowGradientOverRange.Checked

            oInfo.MercuryColourSettingsString = txtMercuryColourSettings.Text
            oInfo.MercuryShadowSettingsString = txtShadowColorSettings.Text
            oInfo.BackGroundSettingsString = txtBackgroundColorSettings.Text

            oInfo.Header = txtHeader.Text
            oInfo.Footer = txtFooter.Text

            oInfo.IMCParameterName = txtIMCParameterName.Text

            oInfo.MatrixColumns = Val(txtMatrixColumns.Text)
            oInfo.MatrixRows = Val(txtMatrixRows.Text)
            If oInfo.MatrixColumns + oInfo.MatrixRows > 0 Then
                oInfo.EnableMatrix = True
            Else
                oInfo.EnableMatrix = False
            End If

            oInfo.ShowSQLCommand = chkShowSQLCommand.Checked
            oInfo.EnableSQL = chkEnableSQL.Checked
            oInfo.SQLCommand = txtSQLCommand.Text
            oInfo.ConnectionString = txtConnectionString.Text

            oInfo.Save()

        End Sub

        Private Sub LoadDropDownLists(ByVal poInfo As ThermoChartInfo)

            Call LoadValueFormatDropDown(poInfo)
            Call LoadScaleLocationDropDown(poInfo)

        End Sub

        Private Sub LoadValueFormatDropDown(ByVal poInfo As ThermoChartInfo)
            ddlValueType.ClearSelection()
            ddlValueType.Items.Clear()

            Dim Types As Array = System.Enum.GetNames(GetType(ThermoChartInfo.ValueFormatType))
            Call Array.Sort(Types)

            ddlValueType.DataSource = Types
            ddlValueType.DataBind()

            Dim oItem As ListItem
            oItem = ddlValueType.Items.FindByText("Integer")
            oItem.Selected = True

            Dim sValue As String = System.Enum.GetName(GetType(ThermoChartInfo.ValueFormatType), poInfo.ValueFormat)
            If sValue.Length > 0 Then
                oItem = ddlValueType.Items.FindByText(sValue)
                If oItem IsNot Nothing Then
                    ddlValueType.ClearSelection()
                    oItem.Selected = True
                End If
            End If

        End Sub

        Private Sub LoadScaleLocationDropDown(ByVal poInfo As ThermoChartInfo)
            ddlScaleLocation.ClearSelection()
            ddlScaleLocation.Items.Clear()

            Dim Types As Array = System.Enum.GetNames(GetType(ThermoChartInfo.ScaleLocationType))
            Call Array.Sort(Types)

            ddlScaleLocation.DataSource = Types
            ddlScaleLocation.DataBind()

            Dim oItem As ListItem
            oItem = ddlScaleLocation.Items.FindByText("Left")
            oItem.Selected = True

            Dim sValue As String = System.Enum.GetName(GetType(ThermoChartInfo.ScaleLocationType), poInfo.ScaleLocation)
            If sValue.Length > 0 Then
                oItem = ddlScaleLocation.Items.FindByText(sValue)
                If oItem IsNot Nothing Then
                    ddlScaleLocation.ClearSelection()
                    oItem.Selected = True
                End If
            End If

        End Sub

        Private Sub PurgeChartImages()

        End Sub

#End Region

    End Class

End Namespace
